package com.yunwaikeji.gy_tool_library.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GYDateUtils {
    /**
     * 日期时间对比
     */
    public static int dateTimeDuiBi(String time0, String time1) {
        return dateTimeDuiBi(time0, time1, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 日期时间对比
     */
    public static int dateTimeDuiBi(String time0, String time1, String format) {
        if (TextUtils.isEmpty(time0)) {
            time0 = "0000-00-00 00:00:00";
        }
        if (TextUtils.isEmpty(time1)) {
            time1 = "0000-00-00 00:00:00";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);//年-月-日 时:分:秒
        try {
            Date date0 = dateFormat.parse(time0);
            Date date1 = dateFormat.parse(time1);
            if (date0.getTime() > date1.getTime()) {
                return 1;
            } else if (date0.getTime() < date1.getTime()) {
                return -1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 日期对比
     */
    public static int dateDuiBi(String time0, String time1) {
        return dateTimeDuiBi(time0, time1, "yyyy-MM-dd");
    }

    /**
     * 计算时间相差天数
     *
     * @param bigTime
     * @param smallTime
     * @return
     */
    public static int getDayDiffer(String bigTime, String smallTime) {
        int days = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时:分:秒
        try {
            Date bigDate = dateFormat.parse(bigTime);
            Date smallDate = dateFormat.parse(smallTime);
            days = (int) ((bigDate.getTime() - smallDate.getTime()) / (1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 计算时间相差 时/分/秒
     *
     * @param bigTime
     * @param smallTime
     * @return
     */
    public static String[] differTime(String bigTime, String smallTime) {
        String[] strings = new String[3];
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时:分:秒
        try {
            Date bigDate = dateFormat.parse(bigTime);
            Date smallDate = dateFormat.parse(smallTime);
            long differTime = bigDate.getTime() - smallDate.getTime();
            int hour = (int) (differTime / (1000 * 60 * 60));
            int minute = (int) (((differTime - hour * (1000 * 60 * 60))) / (1000 * 60));
            int second = (int) (((differTime - hour * (1000 * 60 * 60) - minute * (1000 * 60))) / (1000));
            strings[0] = String.valueOf(GYUtils.toDigit(hour, 2));
            strings[1] = String.valueOf(GYUtils.toDigit(minute, 2));
            strings[2] = String.valueOf(GYUtils.toDigit(second, 2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strings;
    }

    /**
     * 计算时间相差 时/分/秒
     *
     * @param bigTime
     * @param smallTime
     * @return
     */
    public static String[] differTime(String bigTime, String smallTime, String format) {
        String[] strings = new String[3];
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);//年-月-日 时:分:秒
        try {
            Date bigDate = dateFormat.parse(bigTime);
            Date smallDate = dateFormat.parse(smallTime);
            long differTime = bigDate.getTime() - smallDate.getTime();
            int hour = (int) (differTime / (1000 * 60 * 60));
            int minute = (int) (((differTime - hour * (1000 * 60 * 60))) / (1000 * 60));
            int second = (int) (((differTime - hour * (1000 * 60 * 60) - minute * (1000 * 60))) / (1000));
            strings[0] = GYUtils.toDigit(hour, 2);
            strings[1] = GYUtils.toDigit(minute, 2);
            strings[2] = GYUtils.toDigit(second, 2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strings;
    }

    /**
     * 获取当前日期时间
     *
     * @return
     */
    public static String getSystemDateTime() {
        return getSystemDateTime("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当前日期时间
     *
     * @return
     */
    public static String getSystemDate() {
        return getSystemDateTime("yyyy-MM-dd");
    }

    /**
     * 获取当前日期时间
     *
     * @return
     */
    public static String getSystemDateTime(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * 时间转时间戳
     *
     * @param date
     * @return
     */
    public static long date2TimeStamp(String date) {
        return date2TimeStamp(date, "yyyy-MM-dd");
    }

    /**
     * 时间转时间戳
     *
     * @param date
     * @return
     */
    public static long dateTime2TimeStamp(String date) {
        return date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 时间转时间戳
     *
     * @param date
     * @param format
     * @return
     */
    public static long date2TimeStamp(String date, String format) {
        long timeStamp = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            timeStamp = simpleDateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static String timeStamp2Date(Long time) {
        return timeStamp2DateTime(time, "yyyy-MM-dd");
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static String timeStamp2DateTime(Long time) {
        return timeStamp2DateTime(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @param format
     * @return
     */
    public static String timeStamp2DateTime(Long time, String format) {
        String date;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        date = simpleDateFormat.format(new Date(time));
        return date;
    }

    /**
     * 时间戳转 时/分/秒
     *
     * @param time
     * @return
     */
    public static String[] timeStamp2Time(int time) {
        String[] strings = new String[3];
        int hour = (time / (1000 * 60 * 60));
        int minute = (((time - hour * (1000 * 60 * 60))) / (1000 * 60));
        int second = (((time - hour * (1000 * 60 * 60) - minute * (1000 * 60))) / (1000));
        strings[0] = GYUtils.toDigit(hour, 2);
        strings[1] = GYUtils.toDigit(minute, 2);
        strings[2] = GYUtils.toDigit(second, 2);
        return strings;
    }

    /**
     * 计算日期（以今天为基础）
     *
     * @param day 天数，比如：-3表示三天前，3表示三天后
     * @return
     */
    public static String calculateDate(int day) {
        return calculateDate(day, getSystemDate());
    }

    /**
     * 计算日期（以今天为基础）
     *
     * @param day 天数，比如：-3表示三天前，3表示天三天后
     * @return
     */
    public static String calculateDateTime(int day) {
        return calculateDate(day, getSystemDateTime());
    }

    /**
     * 计算日期
     *
     * @param days        天数，比如：-3表示三天前，3表示天三天后
     * @param nowDateTime 当前日期
     * @return
     */
    public static String calculateDate(int days, String nowDateTime) {
        String format;
        if (nowDateTime.length() == 10) {
            format = "yyyy-MM-dd";
        } else {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        long nowLong = date2TimeStamp(nowDateTime, format);
        Long toLong = nowLong + days * 24 * 60 * 60 * 1000;
        return timeStamp2DateTime(toLong, format);
    }

    /**
     * 本周开始日期
     *
     * @return
     */
    public static String getWeekStartDate() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek());
        return calculateDate(1, timeStamp2Date(ca.getTimeInMillis()));
    }

    /**
     * 本月开始日期
     *
     * @return
     */
    public static String getMonthStartDate() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        return timeStamp2Date(ca.getTimeInMillis());
    }

    /**
     * 本年开始日期
     *
     * @return
     */
    public static String getYearStartDate() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_YEAR, 1);
        return timeStamp2Date(ca.getTimeInMillis());
    }

    /**
     * 获取 date 日期 的上一周的开始结束日期，即使给定的日期是周一 也会 计算上一周的日期
     *
     * @param date 基准日期点
     * @return [0]:上一周的开始日期；[1] 上一周的结束日期
     */
    public static String[] getLastWeek(String date) {
        String[] months = new String[2];
        months[1] = calculateDate(-1, getWeekStartDate());
        months[0] = months[1].substring(0, 8) + "01";
        return months;
    }

    /**
     * 获取指定月份开始结束时间
     *
     * @param date
     * @return
     */
    public static String[] getLastMonthDate(String date) {
        String[] months = new String[2];
        months[1] = calculateDate(-1, getMonthStartDate());
        months[0] = months[1].substring(0, 8) + "01";
        return months;
    }
}
